"""
Produce the dataset
"""

import mindspore.dataset as ds
import mindspore.dataset.transforms.c_transforms as C
import mindspore.dataset.transforms.vision.c_transforms as CV
from mindspore.common import dtype as mstype
from .config import alexnet_cfg as cfg

def create_dataset_cifar10(data_path,batch_size=32,repeat_size=1,status="train"):
    """
    create dataset for train or test
    读取cifar10数据的源数据集。
    """
    cifar_ds = ds.Cifar10Dataset(data_path)
    rescale = 1.0/255.0
    shift = 0.0

    resize_op = CV.Resize((cfg.image_height,cfg.image_width))
    rescale_op = CV.Rescale(rescale,shift)  #缩放
    normalize_op = CV.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
    if status == "train":
        random_crop_op = CV.RandomCrop([32,32],[4,4,4,4]) #在随机位置裁剪输入图像。如果提供4个值作为列表或元组，则分别填充左，顶，右和底。
        random_horizontal_op = CV.RandomHorizontalFlip()
    channel_swap_op = CV.HWC2CHW()  #转置输入图像；形状（H，W，C）变形为（C，H，W）
    typecast_op = C.TypeCast(mstype.int32)
    cifar_ds = cifar_ds.map(input_columns="label",operations=typecast_op)
    if status == "train":
        cifar_ds = cifar_ds.map(input_columns="image",operations=random_crop_op)
        cifar_ds = cifar_ds.map(input_columns="image",operations=random_horizontal_op)
    cifar_ds = cifar_ds.map(input_columns="image",operations=resize_op)
    cifar_ds = cifar_ds.map(input_columns="image",operations=rescale_op)
    cifar_ds = cifar_ds.map(input_columns="image",operations=normalize_op)
    cifar_ds = cifar_ds.map(input_columns="image",operations=channel_swap_op)

    cifar_ds = cifar_ds.shuffle(buffer_size=cfg.buffer_size)
    cifar_ds = cifar_ds.batch(batch_size,drop_remainder=True)
    cifar_ds = cifar_ds.repeat(repeat_size)
    return cifar_ds



